package cn.tedu.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

//@Component
public class LogGatewayFilterFactory extends
        AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

    public LogGatewayFilterFactory() {
        super(Config.class);
    }

    //实现具体的判断逻辑
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
               if (config.isConsoleLog()){
                   System.out.println("consoleLog已开启");
               }
                if (config.isCacheLog()){
                    System.out.println("cacheLog已开启");
                }
                //请求继续往下执行   chain链条  exchange请求的目标路径 参数等等
                return chain.filter(exchange);
            }
        };
    }
    //数据的绑定   consoleLog=v    cacheLog=v
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("consoleLog","cacheLog");
    }

    public static class Config{
        //定义两个日志对象 控制台日志  缓存日志
        private boolean consoleLog;
        private boolean cacheLog;

        //读取配置文件中的参数，赋值到配置类中
        public boolean isConsoleLog() {
            return consoleLog;
        }

        public void setConsoleLog(boolean consoleLog) {
            this.consoleLog = consoleLog;
        }

        public boolean isCacheLog() {
            return cacheLog;
        }

        public void setCacheLog(boolean cacheLog) {
            this.cacheLog = cacheLog;
        }
    }
}
